#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "SDLVideoSink.h"
#include "Log.h"
#include "MediaPlayer.h"

SDLVideoSink::SDLVideoSink()
{
	m_WindowWidth = 0;
	m_WindowHeight = 0;
}

SDLVideoSink::~SDLVideoSink()
{

}

int SDLVideoSink::init(int width, int height, int hor_stride, int ver_stride, AVPixelFormat format)
{
	LOGD("width:%d height:%d hor_stride:%d ver_stride:%d format:0x%x", width, height, hor_stride, ver_stride, format);
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {     //初始化视频
        LOGE("Could not initialize SDL - %s", SDL_GetError());
        goto err;
    }
    if(!m_WindowWidth)
    	m_WindowWidth = 1280;
	if(!m_WindowHeight)
    	m_WindowHeight = 720;
    //SDL 2.0 Support for multiple windows
    //创建一个窗口 SDL_Window* screen
    m_Window = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED, m_WindowWidth, m_WindowHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    if (!m_Window) {
        LOGE("SDL: could not create window - exiting:%s", SDL_GetError());
        goto err;
    }
    //创建一个渲染器
    m_Renderer = SDL_CreateRenderer(m_Window, -1, 0);
    if (!m_Renderer) {
        LOGE("SDL: could not create renderer - exiting:%s", SDL_GetError());
        goto err;
    }

	m_Texture = SDL_CreateTexture(m_Renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, width, height);
    if (!m_Texture) {
        LOGE("SDL: could not create texture - exiting:%s", SDL_GetError());
        goto err;
    }
    return 0;
err:
    if (m_Renderer)
        SDL_DestroyRenderer(m_Renderer);
    if (m_Window)
        SDL_DestroyWindow(m_Window);
	return -1;
}

void SDLVideoSink::setDisplayArea(int x, int y, int w, int h)
{
	LOGD("x:%d y:%d w:%d h:%d", x, y, w, h);
	m_DisplayX = x;
	m_DisplayY = y;
	m_DisplayW = w;
	m_DisplayH = h;
}

void SDLVideoSink::setProperty(const char *pro, char *val)
{
	
}

int SDLVideoSink::showFrame(Frame *vframe)
{
	AVFrame *frame = vframe->frame;
	if(!m_Texture || !m_Renderer) {
		LOGE("m_Texture or m_Renderer is NULL");
		return -1;
	}
    SDL_UpdateYUVTexture(m_Texture, NULL, frame->data[0], frame->linesize[0],
                                           frame->data[1], frame->linesize[1],
                                           frame->data[2], frame->linesize[2]);

    //缩放或者放大为w,h
	m_Rect.x = 0; //x,y为矩形左上角的坐标
	m_Rect.y = 0;
	m_Rect.w = 1280;
	m_Rect.h = 720;

	SDL_RenderClear(m_Renderer);
	SDL_RenderCopy(m_Renderer, m_Texture, NULL, &m_Rect);
	SDL_RenderPresent(m_Renderer);
	return 0;
}

//SDL的事件循环，可以在这里捕捉到键盘和鼠标事件进行处理
void SDLVideoSink::eventLoop(void *para)
{
	MediaPlayer *player = (MediaPlayer *)para;
	SDL_Event event;
	int quit = 0;
	int volume = player->getVolume();
	while (!quit) {
	    SDL_WaitEvent(&event);
	    switch (event.type) {
	        case SDL_QUIT://退出事件
	        	LOGD("SDL_QUIT");
	            // SDL_Log("quit");
	            quit = 1;
	            break;
			case SDL_KEYDOWN:
	            switch (event.key.keysym.sym) {
		            case SDLK_LEFT:
		            case SDLK_RIGHT:
		            case SDLK_UP:
		            	player->setVolume(player->getVolume() + 5);
		            	break;
		            case SDLK_DOWN:
		            	player->setVolume(player->getVolume() - 5);
		            	break;
	            }
	            break;
	    }
	}
}